/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tabuvpr.model;

import java.util.ArrayList;

/**
 *
 * @author Shadow
 */
public class Vehiculo {
 
    private boolean _termino =false;
    private int tipoVehiculo; 
    private Node _nodoInicial;
    private Path _ruta;
    private double _consumoPorKm;
    private int _capaciddadInicial;
    private int _capacidadActual;
    private int _velocidad;
    private double _gasolinaIni;
    private double _gasolinaActual;
    
    public Vehiculo(Node nodoInicial){
        _nodoInicial = nodoInicial;
        _ruta = new Path(nodoInicial);
    }
    
    public boolean termino() {
        return _termino;
    }
    public void setTermino(boolean _termino) {
        this._termino = _termino;
    }
    public Node getNodoInicial() {
        return _nodoInicial;
    }
    public void setNodoInicial(Node _nodoInicial) {
        this._nodoInicial = _nodoInicial;
    }
    public Path getRuta() {
        return _ruta;
    }
    public void setRuta(Path _ruta) {
        this._ruta = _ruta;
    }
    public double getConsumoPorKm() {
        return _consumoPorKm;
    }
    public void setConsumoPorKm(double _consumoPorKm) {
        this._consumoPorKm = _consumoPorKm;
    }
    public int getCapaciddadInicial() {
        return _capaciddadInicial;
    }
    public void setCapaciddadInicial(int _capaciddadInicial) {
        this._capaciddadInicial = _capaciddadInicial;
    }
    public int getCapacidadActual() {
        return _capacidadActual;
    }
    public void setCapacidadActual(int _capacidadActual) {
        this._capacidadActual = _capacidadActual;
    }
    public int getVelocidad() {
        return _velocidad;
    }
    public void setVelocidad(int _velocidad) {
        this._velocidad = _velocidad;
    }
    public double getGasolinaIni() {
        return _gasolinaIni;
    }
    public void setGasolinaIni(double _gasolinaIni) {
        this._gasolinaIni = _gasolinaIni;
    }
    public double getGasolinaActual() {
        return _gasolinaActual;
    }
    public void setGasolinaActual(double _gasolinaActual) {
        this._gasolinaActual = _gasolinaActual;
    }

    public int getTipoVehiculo() {
        return tipoVehiculo;
    }

    public void setTipoVehiculo(int tipoVehiculo) {
        this.tipoVehiculo = tipoVehiculo;
    }
   
    
    
    public void move(ArrayList<Node> pedidos){
        Node bestNode = null;
        int nodeIndex = 0;
        double mejorPeso = Double.MAX_VALUE;
        for(int i=0;i< pedidos.size();i++){
            Node nextNode = pedidos.get(i);
            if(!isFeasible(nextNode))continue;
            double pesoActual = calcularPeso(nextNode);
            if (mejorPeso > pesoActual){
                bestNode = nextNode;
                nodeIndex = i;
            }
        }
        if (bestNode==null){
            _termino = true;
            return;
        }
        _ruta.add(bestNode);
        _capacidadActual-=bestNode.getDemanda();
        pedidos.remove(nodeIndex);
    }

    private boolean isFeasible(Node nextNode) {
        boolean capacityConstraint = (nextNode.getDemanda()<= this._capacidadActual);
        if (_ruta.size()==0) return capacityConstraint;
        double distance = Node.distanceBetween(nextNode, _ruta.get(_ruta.size()-1));
        double time = distance/_velocidad;
        boolean timeConstraint = (nextNode.getVentanaTiempoActual()>= time);
        boolean fuelConstraint = (distance * _consumoPorKm < _gasolinaActual);
        return capacityConstraint && fuelConstraint && timeConstraint;    
    }

    private double calcularPeso(Node node) {
        double factorDistancia = (_ruta.size()>0?Node.distanceBetween(node, _ruta.get(_ruta.size()-1)):1);
        double factorCombustible = factorDistancia * _consumoPorKm;
        double factorTiempo = node.getVentanaTiempoActual()- factorDistancia / _velocidad;
        return 1/factorDistancia * 1/factorCombustible + 1/factorTiempo;
    }
}
